package com.hengyi.pda;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.bean.LoginBean;
import com.common.bean.SubmitListBagsBean;
import com.common.broadcastRecever.ScanDataBroadcastReceiver;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.DataCenter;
import com.common.retrofit.ShuangTu.QueryResultBean;
import com.common.retrofit.base.ShuangTuMethods;
import com.common.retrofit.service.ShuangTuService;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.LogUtils;
import com.common.utils.MD5Utils;
import com.common.utils.SPUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hengyi.pda.activity.ModiFyNumActivity;
import com.hengyi.pda.activity.ScanBagsIntrueActivity;
import com.hengyi.pda.activity.Tes;
import com.hengyi.pda.submitjson.SheetSubmitBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

public class YunBiaoPdaActivity extends BaseMvpActivity {


    @BindView(R.id.tv_scan)
    TextView tvScan;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.tv_modify_num)
    TextView tvModifyNum;
    private ScanDataBroadcastReceiver scanDataBroadcastReceiver;
//    private ScanBroadcastReceiver scanBroadcastReceiver; // 新的

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main_pda_yunbiao;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        getToken();
        attachClickListener(tvScan);
        attachClickListener(tvModifyNum);


   /*         TreeSet tree = new TreeSet();
            tree.add("China");
            tree.add("China");
            tree.add("America");
            tree.add("Japan");
            tree.add("Chinese");
            Iterator iter = tree.iterator();
            while(iter.hasNext())
            {
                System.out.println("-------"+iter.next()+"------");
            }*/


    /*     scanBroadcastReceiver = new ScanBroadcastReceiver(new ScanBroadcastReceiver.PdaCallBack() {
            @Override
            public void setinfo(String s) {
                if (isAccept){
                    showToastMsg(s);
                }else {
                    showToastMsg(isAccept+"");
                }
            }
        });
        IntentFilter filter = new IntentFilter();
        filter.addAction("techain.intent.action.DISPLAY_SCAN_RESULT");
        registerReceiver(scanBroadcastReceiver, filter);*/
        //old
        openScanEnable();
        scanOutputDisable();
        scanDataBroadcastReceiver = new ScanDataBroadcastReceiver(new ScanDataBroadcastReceiver.PdaCallBack() {
            @Override
            public void setinfo(String s) {
                if (isAccept) {
                    if (EmptyUtils.isNotEmpty(s)) {
                        showToastMsg(s);
                    }
                }
//                    showToastMsg(s);
            }
        });
        setScanReceiver(scanDataBroadcastReceiver);
    }

    private void getToken() {

        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                String token = (String) o;
//                showToastMsg(token);
                SPUtils.setShareString(DataCenter.TOKEN, token);
                DataCenter.getInstance().setToken(token);

            }

            @Override
            public void onError(String e, int code) {

                showToastMsg(e);
            }
        });

        JsonObject obj = new JsonObject();

        obj.addProperty("id", "sap-test");
        obj.addProperty("password", "sap-test-password");


        String route = new Gson().toJson(obj);
        System.out.println("aaaa" + route);
        LogUtils.e(route);
        RequestBody body = RequestBody.create(MediaType.parse("text/plan; charset=utf-8"), route);
        Observable observable = ShuangTuMethods.getRetrofitToken("http://192.168.0.249:9998/api/").create(ShuangTuService.class).
                auth(body);
        ShuangTuMethods.getInstance().toOtherSubscribe(observable, subscriber);
        rxManager.add(subscriber);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvModifyNum.getId()) {
            gotoLogin();
        } /*else if (view.getId() == tv_modify_num.getId()) {
            gotoActivity(ModiFyNumActivity.class);
        }*/
    }

    private void gotoLogin() {

        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        LoginBean loginBean = new LoginBean() ;
        loginBean.account = getEditTextStr(etName) ;
        loginBean.password = MD5Utils.MD5Encode(getEditTextStr(etPsw)).toUpperCase();
        String route = new Gson().toJson(loginBean);
        LogUtils.e(route);


        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
        Observable observable = ShuangTuMethods.getRetrofit("").create(ShuangTuService.class).
                login(body);
        ShuangTuMethods.getInstance().toOtherSubscribe(observable, subscriber);
        rxManager.add(subscriber);
    }



    @Override
    protected void setNavigation() {
        super.setNavigation();


    }

    /**
     * 按两次返回键退出
     */
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToastMsg("再按一次退出" + getString(R.string.app_name));
                exitTime = System.currentTimeMillis();
                return true;
            } else {
                onBackPressed();
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
/*        if(scanBroadcastReceiver!=null)
            unregisterReceiver(scanBroadcastReceiver);*/
    }


}
